package com.beiing.tianshuai.tianshuai.dongtai.model;

import com.beiing.tianshuai.tianshuai.entity.DynamicPublishBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicPublishImageBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import java.io.File;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 发布活动Model
 * @author Administrator
 */
public class DynamicPublishModel implements DynamicPublishModelImpl{

    private DynamicPublishModel.DynamicPublishTextDataResponseListener mListener;
    private DynamicPublishModel.DynamicPublishImageDataResponseListener mImageListener;

    private static final String TAG = "DynamicPublishModel";

    public DynamicPublishModel(DynamicPublishModel.DynamicPublishTextDataResponseListener listener,
                               DynamicPublishModel.DynamicPublishImageDataResponseListener listenerImage) {
        mListener = listener;
        mImageListener=listenerImage;
    }
    public interface DynamicPublishTextDataResponseListener{
        void onGetDetailTextSuccess(DynamicPublishBean bean);
        void onGetDetailTextError(Throwable throwable);
    }
    public interface DynamicPublishImageDataResponseListener{
        void onGetDetailImageSuccess(DynamicPublishImageBean bean);
        void onGetDetailImageError(Throwable throwable);
    }

    @Override
    public void postDynamicPublishText(String id, String content, String registrationId, String type) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        builder.addFormDataPart("uid", id)
                .addFormDataPart("content",content)
                .addFormDataPart("registration_id",registrationId)
                .addFormDataPart("type",type);
        RequestBody requestBody = builder.build();
        Observable<DynamicPublishBean> observable = DataManager.setDynamicPublishId(requestBody);
        Observable<DynamicPublishBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<DynamicPublishBean>() {
            @Override
            public void onNext(DynamicPublishBean bean) {
                if (mListener != null) {
                    mListener.onGetDetailTextSuccess(bean);
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onGetDetailTextError(e);
                }
            }
            @Override
            public void onComplete() {

            }
        });

    }

    @Override
    public void postDynamicPublishImage(String dId, List<String> imgPaths) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        builder.addFormDataPart("d_id", dId);
        for (String imgPath : imgPaths) {
            File file = new File(imgPath);
            builder.addFormDataPart(
                    "savename[]",
                    file.getName(),
                    RequestBody.create(MediaType.parse("images/*"), file)
            );
        }
        RequestBody requestBody = builder.build();
        Observable<DynamicPublishImageBean> observable = DataManager.setDynamicPublishImage(requestBody);
        Observable<DynamicPublishImageBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<DynamicPublishImageBean>() {
            @Override
            public void onNext(DynamicPublishImageBean bean) {
                if (mListener != null) {
                    mImageListener.onGetDetailImageSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mImageListener.onGetDetailImageError(e);
                }
            }
            @Override
            public void onComplete() {

            }
        });
    }
}
